﻿#ifndef TABLEPDF_H
#define TABLEPDF_H

#include <vector>
#include <string>
#include <istream>
#include <fstream>
#include <sstream>
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
#include <numeric>
#include <QString>

#include "include/hpdf.h"

using namespace std;

struct Pos
{
    Pos():x(0.0), y(0.0) {}
    Pos(HPDF_REAL x1, HPDF_REAL y1):x(x1), y(y1) { }

    HPDF_REAL x, y;
};

struct Rect {
    Rect(): p1(0, 0), p2(0, 0) {}
    Rect(Pos p1, Pos p2): p1(p1), p2(p2) { }

    Pos p1, p2;
};


class TablePdf
{
public:
    TablePdf();
    ~TablePdf();

    Pos switchHaruToCss(Pos p1);
    Pos switchCssToHaru(Pos p1);

    void createPage();

    void setPageMargin(int top, int bottom, int left, int right);

    void setPageProperty(HPDF_PageSizes size, HPDF_PageDirection direction);

    bool setPassword(QString ownerPasswd, QString userPasswd);

    void SetPermission(HPDF_UINT  permission);

    int  watermarkDraw(string imagePath, double trans);

    void drawLine(int pageNum, Pos p1, Pos p2);
    void drawLine(int pageNum,HPDF_REAL x1, HPDF_REAL y1, HPDF_REAL x2, HPDF_REAL y2);

    void drawText(int pageNum, Rect textRect, string text, HPDF_TextAlignment align);
    void drawText(int pageNum, Pos CssPos, Pos sizePos, string text, HPDF_TextAlignment align);

    void addTitle(string titleText, string peopleText);

    bool getTableCsv(const char* csvFileName);

    void drawTable();

    void savePdf(string pdfName);

private:
    HPDF_Doc m_pdf;
    vector<HPDF_Page> m_pageList;
    HPDF_Font m_font;

    // 页面大小横纵
    HPDF_PageSizes pageSize;
    HPDF_PageDirection pageDirection;

    // 页面宽高
    HPDF_REAL m_pageWidth;
    HPDF_REAL m_pageHeight;

    // 页边距
    int m_pageTop;
    int m_pageBottom;
    int m_pageLeft;
    int m_pageRight;

    // 第一页表格 y 坐标
    int m_firstPageTabY;
    int m_firstPageRowSize;
    int m_firstPageDataRowSize;

    int m_commonPageRowSize;
    int m_commonPageDataRowSize;

    size_t m_pageCount;

    HPDF_REAL m_tableWidth;
    HPDF_REAL m_tableHeight;

    HPDF_TextAlignment   m_alignment;

    HPDF_REAL m_tableMargin;

    vector<HPDF_REAL> m_cellWidths;
    HPDF_REAL m_cellHeight;

    // 表头
    vector<string> m_header;

    // 表格数据
    vector<vector<string>> m_tableData;
    size_t m_rowCount;       // 行数
    size_t m_colCount;       // 列数

    void createPage(const int num);
    void computeTableSize();
};

#endif // TABLEPDF_H
